﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QLKS.Templates;
using System.Data;

namespace QLKS.Views
{
    /// <summary>
    /// Interaction logic for frmThemTuyChon.xaml
    /// </summary>
    public partial class frmThemTuyChon : FormTrans
    {
        public bool UpdateSucces = false;
        Command cm;
        DataTable Data = new DataTable();

        public bool IsEditMode
        {
            get { return (bool)GetValue(IsEditModeProperty); }
            set { SetValue(IsEditModeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsEditMode.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsEditModeProperty =
            DependencyProperty.Register("IsEditMode", typeof(bool), typeof(frmThemPhong), new UIPropertyMetadata(true));

        public frmThemTuyChon()
        {
            InitializeComponent();
        }

        public frmThemTuyChon(DataTable tb, Command _cm)
        {
            InitializeComponent();
            IsEditMode = _cm == Command.View ? false : true;
            cm = _cm;
            Data = tb.Copy();
            gridMain.DataContext = Data.DefaultView;
        }

        private void txtGiaTheoGio_GotFocus(object sender, RoutedEventArgs e)
        {
            txtHeSo.SelectAll();
        }

        private void FormTrans_Loaded(object sender, RoutedEventArgs e)
        {
            tbMain.Cm_Save += new ucXToolBar.Execute(tbMain_Cm_Save);
            txtTen.Focus();
            tbMain.Cm_Exit += new ucXToolBar.Execute(tbMain_Cm_Exit);
            pageTuyChon.SetTitle(cm,M_LAN.Equals("V")?"TÙY CHỌN":"OPTION");
        }

        //CheckValid
        private bool KiemTra()
        {

            if (string.IsNullOrEmpty(txtTen.Text))
            {
                MessageBox.Show(M_LAN.Equals("V")?"Chưa vào tên tùy chọn!":"please input option name", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                txtTen.Focus();
                return false;
            }
            else
                if (string.IsNullOrEmpty(txtsoluong.Text))
                {
                    MessageBox.Show(M_LAN.Equals("V")?"Chưa nhập số lượng người giới hạn!":"please insert capacity", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                    txtsoluong.Focus();
                    return false;
                }
                else
                    if (txtHeSo.Value == null)
                    {
                        MessageBox.Show(M_LAN.Equals("V")?"Chưa vào hệ số phụ thu!":"please insert additional rate", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                        txtHeSo.Focus();
                        return false;
                    }

            return true;
        }

        //Save
        void tbMain_Cm_Save()
        {
            //Thêm vào CSDL dữ liệu

            if (KiemTra())
            {
                if (ViewModels.TuyChonViewModel.Update(Data))
                {
                    this.UpdateSucces = true;
                    if (cm == Command.Edit)
                        MessageBox.Show(M_LAN.Equals("V")?"Cập nhật thành công!":"Update successful", "HotelManager", MessageBoxButton.OK, MessageBoxImage.Information);
                    else
                    {
                        MessageBox.Show(M_LAN.Equals("V")?"Thêm thành công!":"Insert successful", "HotelManager", MessageBoxButton.OK, MessageBoxImage.Information);
                        cm = Command.Edit;
                    }
                    //Close();
                }
            }
        }

        //Thoát
        void tbMain_Cm_Exit()
        {
            this.Close();
        }

    }
}
